package com.i2works.smartluolong.api.controller.user;

import com.i2works.smartluolong.api.utils.BaseModule;
import com.i2works.smartluolong.utils.base.MsgEnum;
import com.i2works.smartluolong.utils.common.BaseUtils;
import com.i2works.smartluolong.utils.entity.R;
import com.i2works.smartluolong.utils.entity.Result;
import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.web.entity.user.UserAccountQuery;
import com.i2works.smartluolong.web.entity.user.UserAccountVO;
import com.i2works.smartluolong.web.entity.user.UserLoginVO;
import com.i2works.smartluolong.web.service.user.IUserAccountExtendService;
import com.i2works.smartluolong.web.service.user.IUserAccountService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Liu Yutao <koal@vip.qq.com>
 * @date 2018/1/30 15:01
 */
@RestController
@RequestMapping("/user")
public class UserAccountController extends BaseModule {
/*
    private final IUserAccountService userAccountService;


    private final IUserAccountExtendService userAccountExtendService;

    @Autowired
    public UserAccountController(IUserAccountService userAccountService, IUserAccountExtendService userAccountExtendService) {
        this.userAccountService = userAccountService;
        this.userAccountExtendService = userAccountExtendService;
    }


    *//**
     * 用户登录方法
     *
     * @param userName 用户名或手机号
     * @param password 密码
     *//*
    @PostMapping("/login")
    public Result login(@RequestParam(value = "userName", required = false) String userName,
                        @RequestParam(value = "password", required = false) String password) {
        //判断账号密码是否为空
        if (StringUtils.isAnyBlank(userName, password)) {
            return R.error(MsgEnum.ACCESS_EMPTY.getMsg());
        }
        try {
            UserLoginVO userLoginVO = new UserLoginVO();
            userLoginVO.setUserName(userName);
            userLoginVO.setPhone(userName);
            userLoginVO.setPassword(password);
            return userAccountService.queryAppLoginUser(userLoginVO);
        } catch (ServiceException e) {
            logger.error("登录异常：" + e.getMessage());
            return R.error(MsgEnum.ACCESS_LOGIN_FAIL.getMsg());
        }
    }


    *//**
     * 用户找回密码
     *//*
    @PostMapping("/resetPassword")
    public Result password(@ModelAttribute UserLoginVO userLoginVO) {
        return updatePassword(userLoginVO);
    }

    *//**
     * 用户注册方法
     *//*
    @PostMapping("/register")
    public Result register(@ModelAttribute UserLoginVO userLoginVO, HttpServletRequest request) {
        try {
            //实体
            if (null == userLoginVO) {
                return R.error(MsgEnum.ATTRS_EMPTY.getMsg());
            }

            //手机号
            if (StringUtils.isBlank(userLoginVO.getPhone())) {
                return R.error(MsgEnum.ACCESS_PHONE_NULL.getMsg());
            }

            //账号密码
            if (StringUtils.isAnyBlank(userLoginVO.getUserName(), userLoginVO.getPassword())) {
                return R.error(MsgEnum.ACCESS_USERPHOME_EMPTY.getMsg());
            }

            //两次密码对比
            if (StringUtils.isNotBlank(userLoginVO.getConfirmPassword())) {
                if (!StringUtils.equalsIgnoreCase(userLoginVO.getPassword(), userLoginVO.getConfirmPassword())) {
                    return R.error(MsgEnum.ACCESS_CONFIRMPASSWORD_ERROR.getMsg());
                }
            }

            //短信验证码
            Result result = checkSmsCode(userLoginVO.getPhone(), userLoginVO.getCode());
            if (!result.getSuccess()) {
                return result;
            }

            //判断账号是否存在
            UserAccountQuery userAccountQuery = new UserAccountQuery();
            userAccountQuery.setUserName(userLoginVO.getUserName());
            userAccountQuery.setPhone(userLoginVO.getPhone());
            int queryCheck = userAccountService.queryCheck(userAccountQuery);
            if (queryCheck > 0) {
                return R.error(MsgEnum.ACCESS_USERPHOME_KNOW.getMsg());
            } else {
                //注册
                userLoginVO.setCreateName(userLoginVO.getUserName());
                userLoginVO.setUpdateName(userLoginVO.getUserName());
                userLoginVO.setLoginIp(BaseUtils.getIP(request));
                Integer saveStatus = userAccountService.saveRegisterUser(userLoginVO);
                if (saveStatus > 0) {
                    UserAccountVO userAccountVO = new UserAccountVO();
                    BeanUtils.copyProperties(userLoginVO, userAccountVO);
                    return R.success(MsgEnum.ACCESS_REG_SUCCESS.getMsg(), userAccountVO);
                } else {
                    return R.error(MsgEnum.ACCESS_REG_FAIL.getMsg());
                }
            }
        } catch (Exception e) {
            logger.error("注册异常：" + e.getMessage());
            return R.error(MsgEnum.ACCESS_REG_FAIL.getMsg());
        }
    }


    *//**
     * 账号元素占用检测
     *
     * @param checkType 1账号,2邮箱,3手机号，0全部
     * @param param     账号/邮箱/手机号
     * @return 存在返回false, 不存在返回true
     *//*
    @PostMapping("/check")
    public Result check(@RequestParam Integer checkType, @RequestParam String param) {
        try {
            checkType = checkType == null ? 0 : checkType;
            if (StringUtils.isNotBlank(param)) {
                UserAccountQuery userQuery = new UserAccountQuery();
                switch (checkType) {
                    case 0:
                        userQuery.setSearch(param);
                        break;
                    case 1:
                        userQuery.setUserName(param);
                        break;
                    case 2:
                        userQuery.setEmail(param);
                        break;
                    case 3:
                        userQuery.setPhone(param);
                        break;
                    default:
                        userQuery.setSearch(param);
                        break;
                }
                int checkCount = userAccountService.queryCheck(userQuery);
                Result success = R.success();
                if (checkCount > 0) {
                    success.setSuccess(false);
                    success.setMsg(MsgEnum.ATTRS_IS_USED.getMsg());
                }
                return success;
            }
        } catch (Exception e) {
            logger.error("账号元素占用检测方法异常:", e);
        }
        return R.error();
    }
*/

}
